// pages/order/detail.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this,
      id = options.id

    wx.showLoading({
      title: '加载中',
    })

    req.request(
      api.getUserOrderById,
      { method: 'POST', data: { id: id } },
      function (res) {
        wx.hideLoading();
        res = res.data

        _this.setData({ order_info: res })
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  check(e) {
    var _this = this,
        id = _this.data.order_info.order_info.order_id,
        count = _this.data.order_info.person_list.length,
        index = e.currentTarget.id
    
    if(index == 1) {
      wx.showModal({
        title: '请确认',
        content: '需要出具成人票' + count + '张',
        confirmText: '好的',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '加载中',
            })
            req.request(
              api.checkUserOrder,
              { method: 'POST', data: { id: id } },
              function (res) {
                wx.hideLoading();
                res = res.data

                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })

                if (res.status == 1) {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 2000)
                }
              }
            )
          }
        }
      })
    }else if(index == 2) {
      wx.showModal({
        title: '请确认',
        content: '用户已取到票',
        confirmText: '我已确认',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '加载中',
            })
            req.request(
              api.userGotTicket,
              { method: 'POST', data: { id: id } },
              function (res) {
                wx.hideLoading();
                res = res.data

                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })

                if (res.status == 1) {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 2000)
                }
              }
            )
          }
        }
      })
    }else if(index == 3) {
      wx.navigateBack({
        delta: 1
      })
    }
    
  }
})